package action;

import action.base.BaseAction;

import java.io.ByteArrayInputStream;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import util.SecurityCode;
import util.SecurityImage;

/**
 * 提供图片验证码
 * 
 * @version 1.0 2014年11月18日
 * @author wyk
 */
@Controller
@Scope("prototype")
public class SecurityCodeImageAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Struts2中Map类型的session
	private Map<String, Object> session;
	private boolean success = false;
	// 图片流
	private ByteArrayInputStream imageStream;

	public ByteArrayInputStream getImageStream() {
		return imageStream;
	}

	public void setImageStream(ByteArrayInputStream imageStream) {
		this.imageStream = imageStream;
	}

	public String execute() throws Exception {
		// 如果开启Hard模式，可以不区分大小写
		// String securityCode =
		// SecurityCode.getSecurityCode(4,SecurityCodeLevel.Hard,
		// false).toLowerCase();

		// 获取默认难度和长度的验证码
		String securityCode = SecurityCode.getSecurityCode();
		imageStream = SecurityImage.getImageAsInputStream(securityCode);
		// 放入session中
		// session.put("SECURITY_CODE", securityCode);
		session.put("SESSION_SECURITY_CODE", securityCode);
		setSuccess(true);
		return "success";
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Map<String, Object> getSession() {
		return session;
	}

}